using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using DTO;
using System.Data;
using System.Data.OleDb;


namespace BLL
{
    public class DiemDAL
    {
        public List<DIEMTHI> GetAll()
        {
            DIEMTHI diem;
            List<DIEMTHI> list = new List<DIEMTHI>();
            DataProvider dataprovider = new DataProvider();
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from DIEMTHI", null);
           
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    diem= new DIEMTHI();
                    diem.Mamon = Convert.ToByte(datatable.Rows[i]["Mamon"]);
                    diem.MaSV = datatable.Rows[i]["MaSV"].ToString();
                    diem.Diem = Convert.ToByte(datatable.Rows[i]["Diem"]);
                    list.Add(diem);
                }
            return list;
        }

        public DIEMTHI GetDIEMTHI(Int32 masv)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@masv", OleDbType.Integer);
            parameters[0].Value = masv;
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from DIEMTHI where MaSV = @masv", parameters);
            DIEMTHI diemthi = new DIEMTHI();
            if (datatable.Rows.Count != 0)
            {
                diemthi.MaSV = datatable.Rows[0]["MaSV"].ToString();
                diemthi.Mamon = Convert.ToByte(datatable.Rows[0]["Mamon"]);
                diemthi.Diem = Convert.ToDecimal(datatable.Rows[0]["Diem"]);
                return diemthi;
            }
            else
                return null;
        }

        public bool UpdateDIEM(Int32 masv, byte diem, Int32 masvcu)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[3];
            parameters[0] = new OleDbParameter("@masv", OleDbType.Integer);
            parameters[0].Value = masv;
            parameters[1] = new OleDbParameter("@diem", OleDbType.Char);
            parameters[1].Value = diem;
            parameters[2] = new OleDbParameter("@masvcu", OleDbType.Integer);
            parameters[2].Value = masvcu;
            return dataprovider.ExecuteNonSelectQuery("update DIEMTHI set MaSV=@masv, Diem=@diem where MaSV = @masvcu", parameters);
        }
    }
}
